package controller;

import com.alibaba.dubbo.config.annotation.Reference;
import entity.ResponseResult;
import entity.Users;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import service.UserService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: lagou-dubbo
 * @Author: GuoAn.Sun
 * @CreateTime: 2020-07-27 18:16
 * @Description: 控制层
 */
@RestController
public class UserAction {

    @Reference
    private UserService userService;

    @RequestMapping("/register")
    public ResponseResult register(@RequestBody Users user) {

        try {
            if ( user.getUid() == null ) {
                String nowTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
                user.setCreatetime(nowTime);
                userService.register(user);
                ResponseResult responseResult = new ResponseResult(true, 200, "响应成功", user);
                return responseResult;
            }else{
                userService.updateUser(user);
                ResponseResult responseResult = new ResponseResult(true,200,"响应成功",null);
                return responseResult;
            }

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping("/findAllUsers")
    public ResponseResult findAllUsers(@RequestBody Users users){
        List<Users> allUser = userService.findAllUser(users);
        ResponseResult responseResult = new ResponseResult(true,200,"响应成功",allUser);
        return responseResult;
    }

    @RequestMapping("/deleteUser")
    public  ResponseResult deleteUser(Integer id) {
        userService.deleteUser(id);
        ResponseResult responseResult = new ResponseResult(true,200,"响应成功",null);
        return responseResult;
    }
    @RequestMapping("/findUserById")
    public ResponseResult findUserById(Integer id) {
        Users userById = userService.findUserById(id);
        ResponseResult responseResult = new ResponseResult(true,200,"响应成功",userById);
        return responseResult;
    }





}
